import 'package:flutter/material.dart';
import 'package:flutter_easyloading/flutter_easyloading.dart';
import 'package:lazy_food/degrease/degrease.dart';
import 'package:lazy_food/favorite/favorite.dart';
import 'package:lazy_food/home/home.dart';
import 'package:lazy_food/mine/mine.dart';

void main() {
  runApp(const MyApp());
}

class MyApp extends StatelessWidget {
  const MyApp({Key? key}) : super(key: key);

  // This widget is the root of your application.
  @override
  Widget build(BuildContext context) {
    return MaterialApp(
      title: '懒饭',
      themeMode: ThemeMode.dark,
      darkTheme: ThemeData(
        primaryColor: Colors.black,
        highlightColor: Colors.transparent,
        splashColor: Colors.transparent,
      ),
      home: AppPage(),
      builder: EasyLoading.init(),
    );
  }
}

class AppPage extends StatefulWidget {
  @override
  State<StatefulWidget> createState() => AppPageState();
}

class AppPageState extends State<AppPage> with SingleTickerProviderStateMixin {
  List<BottomNavigationBarItem> _items = [
    BottomNavigationBarItem(
        icon: Image.asset(
          "assets/images/tabADeselected.png",
          color: Colors.white54,
          excludeFromSemantics: true, //去除图片语义
          gaplessPlayback: true,
        ),
        activeIcon: Image.asset(
          "assets/images/tabASelected.png", color: Colors.white,
          excludeFromSemantics: true, //去除图片语义
          gaplessPlayback: true,
        ),
        label: '首页'),
    BottomNavigationBarItem(
        icon: Image.asset(
          "assets/images/tabBDeselected.png",
          color: Colors.white54,
          excludeFromSemantics: true, //去除图片语义
          gaplessPlayback: true,
        ),
        activeIcon: Image.asset(
          "assets/images/tabBSelected.png",
          color: Colors.white,
          excludeFromSemantics: true, //去除图片语义
          gaplessPlayback: true,
        ),
        label: '减脂'),
    BottomNavigationBarItem(
        icon: Image.asset(
          "assets/images/tabCDeselected.png",
          color: Colors.white54,
          excludeFromSemantics: true, //去除图片语义
          gaplessPlayback: true,
        ),
        activeIcon: Image.asset(
          "assets/images/tabCSelected.png",
          color: Colors.white,
          excludeFromSemantics: true, //去除图片语义
          gaplessPlayback: true,
        ),
        label: '收藏'),
    BottomNavigationBarItem(
        icon: Image.asset(
          "assets/images/tabDDeselected.png",
          color: Colors.white54,
          excludeFromSemantics: true, //去除图片语义
          gaplessPlayback: true,
        ),
        activeIcon: Image.asset(
          "assets/images/tabDSelected.png", color: Colors.white,
          excludeFromSemantics: true, //去除图片语义
          gaplessPlayback: true,
        ),
        label: '我的'),
  ];

  List<Widget> _pages = [
    HomePage(),
    DegreasePage(),
    FavoritePage(),
    MinePage()
  ];
  int _currentPage = 0;
  late TabController _tabController;

  void _changeTab(int page) {
    setState(() {
      _currentPage = page;
    });
    _tabController.animateTo(_currentPage);
  }

  @override
  void initState() {
    // TODO: implement initState
    super.initState();
    _tabController = TabController(length: _pages.length, vsync: this);

    // 自定义toast样式
    EasyLoading.instance
      ..loadingStyle = EasyLoadingStyle.dark
      ..maskType = EasyLoadingMaskType.black
      ..backgroundColor = Colors.white
      ..maskColor = Colors.white
      ..radius = 10.0
      ..dismissOnTap = false;
  }

  @override
  Widget build(BuildContext context) {
    return Scaffold(
      body: TabBarView(
        children: _pages,
        physics: NeverScrollableScrollPhysics(),
        controller: _tabController,
      ),
      bottomNavigationBar: BottomNavigationBar(
        type: BottomNavigationBarType.fixed,
        items: _items,
        currentIndex: _currentPage,
        unselectedItemColor: Colors.white54,
        selectedItemColor: Colors.white,
        backgroundColor: Colors.black87,
        onTap: _changeTab,
      ),
    );
  }
}
